#include "CEUtilities.h"

#include "CEMouse.h"
#include "CESoundManager.h"
#include "CEGUILabel.h"
#include "CEGUIPictureBox.h"
#include "CEGUIButton.h"


CEGUIButton::CEGUIButton(CEGUIElementBase * parent, int id, const string& name, int offsetX, int offsetY, int width, int height,
						 const string& imageNormal, const string& imageOver, const string& imageButtonDown, 
						 const string& imageDisable, const string& strText, 
						 bool visible, int tabindex, bool tabenable, bool bPushed) 
		 : CEGUIButtonBase (parent, id, name, imageNormal, imageOver, imageButtonDown,
		 imageDisable, strText, offsetX, offsetY, width, height, visible, tabindex, tabenable)
		 ,m_pEventHandler(NULL), m_pBtnSound(NULL), _bOnBtn(false)
{
	m_eType = GUI_Button;

	m_pPicNormal->SetUVMap(0, 0, 1, 0.25f);
	m_pPicMouseOver->SetUVMap(0, 0.25f, 1, 0.5f);
	m_pPicButtonDown->SetUVMap(0, 0.5f, 1, 0.75f);
	m_pPicButtonDisable->SetUVMap(0, 0.75f, 1, 1);

	_bGUIEditPosControled = false;
	_bGUIEditSizeHControled = false;
	_bGUIEditSizeVControled = false;
	_bGUIEditSizeHVControled = false;
}


CEGUIButton::~CEGUIButton()
{
	SAFE_DELETE(m_pEventHandler);
	SetBtnSoundNull();
}


void CEGUIButton::SetBtnSound(const string& sound)
{
	if (m_pBtnSound != NULL)
	{
		if (m_pBtnSound->GetOrigin() != sound)
		{
			CESoundManager::GetSingleton()->RemoveResource(m_pBtnSound->GetHandle());
		}
		else
		{
			return;
		}
	}

	m_pBtnSound = CESoundManager::GetSingleton()->Create2DSound(sound);
}


void CEGUIButton::SetBtnSoundNull()
{
	if (m_pBtnSound != NULL)
	{
		CESoundManager::GetSingleton()->RemoveResource(m_pBtnSound->GetHandle());
		m_pBtnSound = NULL;
	}
}


HRESULT CEGUIButton::Update(float deltaTime)
{
	if (!m_bVisible)
		return S_OK;

	if(GetButtonState() != BTS_DISABLE)
	{
		if(CheckMousePosition())
		{
			switch(GetButtonState())
			{
			case BTS_NORMAL:
				if ( !Mouse::GetSingleton()->IsMouseKeyDown(0, false) )
				{
					SetButtonStatus(BTS_MOUSEOVER);
					if (!_bOnBtn)
					{
						_bOnBtn = true;
						if (m_pBtnSound)
							m_pBtnSound->PlayMusic(CESound::RESUME_ONCE);
					}
				}
				break;
			case BTS_MOUSEOVER:
				if ( Mouse::GetSingleton()->IsMouseKeyDown(0, true) )
					SetButtonStatus(BTS_BUTTONDOWN);
				break;
			case BTS_BUTTONDOWN:
				if ( Mouse::GetSingleton()->IsMouseKeyUp(0, true) )
				{
					SetButtonStatus(BTS_MOUSEOVER);

					if (m_pEventHandler)
						m_pEventHandler->Execute();
				}
				break;
			default:
				break;
			}
		}
		else
		{
			this->SetButtonStatus(BTS_NORMAL);
			_bOnBtn = false;
		}
	}
	
	return S_OK;

}

bool CEGUIButton::CheckMousePosition()
{
	if	(CEUtil::DotInRect(Mouse::GetSingleton()->GetX(),Mouse::GetSingleton()->GetY(),
		GetPosition().x, GetPosition().y, GetSize().x, GetSize().y))
		return true;
	else
		return false;

}

void CEGUIButton::BindFunction(void (*func)())
{
	SAFE_DELETE(m_pEventHandler);
	m_pEventHandler = CreateStaticEventHandler(func);
}


void CEGUIButton::SaveInScrips(ofstream &fout, int level)
{
	string blank = "";
	int temp = level;
	while (temp--) blank += "\t\t";

	fout << endl;
	fout << blank << "<element_begin>" << endl;
	fout << blank << "\t" << "<Button>" << endl;

	if (m_strName != CEUtil::IntToStr(m_nID))
		fout << blank << "\t\t" << "<name>" << "\t" << m_strName << endl;
	fout << blank << "\t\t" << "<size>" << "\t" << m_Size.x << "\t" << m_Size.y << endl;
	fout << blank << "\t\t" << "<offset>" << "\t" << m_OffsetFromParent.x << "\t" << m_OffsetFromParent.y << endl;
	fout << blank << "\t\t" << "<visible>" << "\t" << m_bVisible << endl;
	fout << blank << "\t\t" << "<enable>" << "\t" << m_bEnable << endl;
	fout << blank << "\t\t" << "<level>" << "\t" << m_iShowLevel << endl;

	CEGUIPictureBox::SaveInScrips(m_pPicNormal, fout, level+1);
	CEGUIPictureBox::SaveInScrips(m_pPicMouseOver, fout, level+1);
	CEGUIPictureBox::SaveInScrips(m_pPicButtonDown, fout, level+1);
	CEGUIPictureBox::SaveInScrips(m_pPicButtonDisable, fout, level+1);

	CEGUILabel::SaveInScrips(m_pLabel, fout, level+1);

	fout << blank << "<element_end>" << endl;
}



void CEGUIButton::SaveInScrips(CEGUIButton* button, ofstream &fout, int level)
{
	if (button)
	{
		button->SaveInScrips(fout, level);
	}
	else
	{
		string blank = "";
		int temp = level;
		while (temp--) blank += "\t\t";
		fout << endl;
		fout << blank << "<element_begin>" << endl;
		fout << blank << "\t" << "<Button>" << endl;
		fout << blank << "\t\t" << "<NULL>" << endl;
		fout << blank << "<element_end>" << endl;
	}
}